package com.xxkk.spring_boot_web.sdk.KBZ.utils;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.Security;
import java.util.Base64;

/* renamed from: com.huawei.kbz.utils.encrypt.AESUtil */
/* loaded from: classes9.dex */
public class AESUtil {
    private static int KEYSIZE = 256;

    static {
        Security.addProvider(new BouncyCastleProvider());//让系统支持 PKCS7Padding加密
    }

    public static String decrypt(String str, String str2, String str3) {
        try {
            SecretKeySpec secretKeySpec = new SecretKeySpec(Base64.getDecoder().decode(str2), "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
            cipher.init(2, secretKeySpec, new IvParameterSpec(str3.getBytes()));
            return new String(cipher.doFinal(Base64.getDecoder().decode(str)), "UTF-8");
        } catch (Exception unused) {
            return null;
        }
    }

    public static String encrypt(String str, String str2, String str3) {
        try {
            SecretKeySpec secretKeySpec = new SecretKeySpec(Base64.getDecoder().decode(str2), "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
            cipher.init(1, secretKeySpec, new IvParameterSpec(str3.getBytes()));
            return Base64.getEncoder().encodeToString(cipher.doFinal(str.getBytes()));
        } catch (Exception unused) {
            unused.printStackTrace();
            return null;
        }
    }

    public static String genarateRandomKey() {
        try {
            KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
            keyGenerator.init(KEYSIZE);
            return Base64.getEncoder().encodeToString(keyGenerator.generateKey().getEncoded());
        } catch (Exception unused) {
            return null;
        }
    }
}
